﻿// Code obtained thanks to http://www.c-sharpcorner.com/UploadFile/ae35ca/working-with-camera-in-windows-phone-7/

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Devices;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework.Media;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Microsoft.Phone;

namespace GenetecHackathonCod3
{
    public partial class TakePicture : PhoneApplicationPage
    {
        private CameraCaptureTask ccTask;
        public Boolean saveButton = false;
        private DateTime date;
        public TakePicture()
        {
          InitializeComponent();
          ccTask = new CameraCaptureTask();
          ccTask.Completed += ccTaskCompleted;
        }
        private void btnCamera_Click(object sender, RoutedEventArgs e)
        {
         ccTask.Show();
        }

        private void ccTaskCompleted(object sender, PhotoResult pr)
        {
            saveButton = true;
            byte[] imgLocal;
            if (pr.ChosenPhoto != null)
            {
                imgLocal = new byte[(int)pr.ChosenPhoto.Length];
                pr.ChosenPhoto.Read(imgLocal, 0, imgLocal.Length);
                pr.ChosenPhoto.Seek(0, System.IO.SeekOrigin.Begin);
                var bitmapImage = PictureDecoder.DecodeJpeg(pr.ChosenPhoto);
                this.imgCaptured.Source = bitmapImage;
            }
        }

        public void Save_click(object sender, Stream e)
        {
            SaveImage(e, "test", 1, 1);
            EditReport.currentReport.appendSound(new Recording("" + date.Second + date.Minute + date.Hour + date.Day + date.Month + date.Year + ".wav"));
            NavigationService.Navigate(new Uri("/EditReport.xaml", UriKind.Relative));
            MainPage.save();
        }

            public static void SaveImage(Stream imageStream, string fileName, int orientation, int quality)
{
    using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (isolatedStorage.FileExists(fileName))
            isolatedStorage.DeleteFile(fileName);

        IsolatedStorageFileStream fileStream = isolatedStorage.CreateFile(fileName);
        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(imageStream);

        WriteableBitmap wb = new WriteableBitmap(bitmap);
        wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, orientation, quality);
        fileStream.Close();
    }
}
        }


    }
